package spring.data.jpa.repository.support;

import java.util.ArrayList;
import java.util.List;

import spring.data.jpa.repository.support.SearchFilter.Operator;

public class FilterSupport {
	protected List<SearchFilter> andFilter = new ArrayList<>();
	protected List<SearchFilter> orFilter = new ArrayList<>();

	public List<SearchFilter> getAndFilter() {
		return andFilter;
	}

	public List<SearchFilter> getOrFilter() {
		return orFilter;
	}

	public void and(SearchFilter filter) {
		this.andFilter.add(filter);
	}

	public void and(String field, Operator operator, Object... value) {
		switch (operator) {
		case IN:
			this.andFilter.add(new SearchFilter(field, operator, value));
			break;
		case ISNULL:
			this.andFilter.add(new SearchFilter(field, operator, null));
			break;
		case ISNOTNULL:
			this.andFilter.add(new SearchFilter(field, operator, null));
			break;
		default:
			this.andFilter.add(new SearchFilter(field, operator, value[0]));
			break;
		}
	}

	public void or(SearchFilter filter) {
		this.orFilter.add(filter);
	}

	public void or(String field, Operator operator, Object... value) {
		switch (operator) {
		case IN:
			this.orFilter.add(new SearchFilter(field, operator, value));
			break;
		case ISNULL:
			this.orFilter.add(new SearchFilter(field, operator, null));
			break;
		case ISNOTNULL:
			this.orFilter.add(new SearchFilter(field, operator, null));
			break;
		default:
			this.orFilter.add(new SearchFilter(field, operator, value[0]));
			break;
		}
	}
}
